Component({
  properties: {
    img: null,
    qrcode: null,
    //属性值可以在组件使用时指定
    isCanDraw: {
      type: Boolean,
      value: false,
      observer(newVal, oldVal) {
        // newVal && this.drawPic()
      }
    }
  },
  data: {
    isModal: false, //是否显示拒绝保存图片后的弹窗
    imgDraw: {}, //绘制图片的大对象
    sharePath: '', //生成的分享图
    visible: true,
    isSave: false
  },
  methods: {
    saveImg: function () {
      if (this.data.sharePath) {
        this.savePhoto(this.data.sharePath)
      } else {
        this.drawPic()
      }

    },
    drawPic() {
      if (this.data.sharePath) { //如果已经绘制过了本地保存有图片不需要重新绘制
        this.setData({
          visible: true
        })
        this.triggerEvent('initData')
      }
      wx.showLoading({
        title: ''
      })
      this.setData({
        imgDraw: {
          width: '650rpx',
          height: '863rpx',
          borderRadius: '30rpx',
          background: this.properties.img,
          views: [{
              type: 'image',
              url: this.properties.qrcode,
              css: {
                borderRadius: '10rpx',
                top: '635rpx',
                left: '456rpx',
                right: '20rpx',
                width: '200rpx',
                height: '200rpx',

              },
            },

            {
              type: 'text',
              text: '手机微信扫码',
              css: {
                top: '735rpx',
                fontSize: '36rpx',
                left: '20rpx',
                align: 'left',
                color: '#fff'
              }
            },
            {
              type: 'text',
              text: '打开易学派商城',
              css: {
                top: '800rpx',
                fontSize: '26rpx',
                left: '20rpx',
                align: 'left',
                color: '#fff'
              }
            }
          ]
        }
      })
    },
    onImgErr(e) {
      wx.hideLoading()
      wx.showToast({
        title: '生成分享图失败，请刷新页面重试'
      })
    },
    onImgOK(e) {
      wx.hideLoading()
      console.log(" e.detail.path:" + e.detail.path)
      this.setData({
        sharePath: e.detail.path,
      })
      // if (this.data.isSave) {
        this.savePhoto(e.detail.path)
      // }
    },
    preventDefault() {},
    // 保存图片
    savePhoto(path) {
      wx.showLoading({
        title: '正在保存...',
        mask: true
      })
      // this.setData({
      //   isDrawImage: false
      // })
      wx.saveImageToPhotosAlbum({
        filePath: path,
        success: (res) => {
          wx.showToast({
            title: '保存成功',
            icon: 'none'
          })
          setTimeout(() => {
            // this.setData({
            //   visible: false
            // })
          }, 300)
        },
        fail: (res) => {
          console.log("fail:" + res.errMsg)
          wx.getSetting({
            success: res => {
              let authSetting = res.authSetting
              if (!authSetting['scope.writePhotosAlbum']) {
                this.setData({
                  isModal: true
                })
              }
            }
          })
          setTimeout(() => {
            wx.hideLoading()
            // this.setData({
            //   visible: false
            // })
          }, 1000)
        }
      })
    }
  }
})